/*
 * Copyright 2010 Okan Kahraman
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package tr.org.tx.vmot.ui.calendar;

import java.text.DateFormatSymbols;
import java.util.Calendar;
import java.util.GregorianCalendar;

import tr.org.tx.vmot.ui.base.AbstractView;

import com.vaadin.terminal.ThemeResource;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.themes.BaseTheme;

/**
 * @author Okan Kahraman
 *
 */
public class VmotCalendarNavigator extends AbstractView {

	private static final long serialVersionUID = 2096121834756006889L;
	
	private final HorizontalLayout layout = new HorizontalLayout();
	
	private final Label month = new Label();

	private final Label year = new Label();
	
	private NavigableCalendar navigableCalendar;
	
	private DateFormatSymbols dateFormatSymbols = DateFormatSymbols.getInstance(getCurrentLocale());
	
	public 	VmotCalendarNavigator(NavigableCalendar navigableCalendar) {
		this.navigableCalendar = navigableCalendar;
		setLayOutProperties();
		constructCalendarNavigator();
	}

	@SuppressWarnings("serial")
	private void constructCalendarNavigator() {
		Button previousMonth = new Button();
		previousMonth.setStyleName(BaseTheme.BUTTON_LINK);
		previousMonth.setIcon(new ThemeResource("icons/32/prev.png"));
		previousMonth.setDescription(getMessage("calendar.navigator.previous.month"));
		
		Button nextMonth = new Button();
		nextMonth.setStyleName(BaseTheme.BUTTON_LINK);
		nextMonth.setIcon(new ThemeResource("icons/32/next.png"));
		nextMonth.setDescription(getMessage("calendar.navigator.next.month"));
		
		
		month.setValue(navigableCalendar.getCurrentMonthName());
		month.setStyleName("month");
		year.setValue(navigableCalendar.getCurrentYear());
		year.setStyleName("year");
		
		layout.addComponent(previousMonth);
		layout.setComponentAlignment(previousMonth, Alignment.MIDDLE_CENTER);
		layout.addComponent(month);
		layout.setComponentAlignment(month, Alignment.MIDDLE_LEFT);
		layout.addComponent(year);
		layout.setComponentAlignment(year, Alignment.MIDDLE_RIGHT);
		layout.addComponent(nextMonth);
		layout.setComponentAlignment(nextMonth, Alignment.MIDDLE_CENTER);
				
		previousMonth.addListener(new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				navigableCalendar.previousMonth();
				month.setValue(navigableCalendar.getCurrentMonthName());
				year.setValue(navigableCalendar.getCurrentYear());
			}
		});
		
		nextMonth.addListener(new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				navigableCalendar.nextMonth();
				month.setValue(navigableCalendar.getCurrentMonthName());
				year.setValue(navigableCalendar.getCurrentYear());
			}
		});
	}

	private void setLayOutProperties() {
		layout.setSpacing(true);
	    layout.setMargin(true);
	    layout.setWidth("1000px");
	    layout.setHeight("70px");
	    layout.setStyleName("calendarNavigator");
	    setCompositionRoot(layout);
	}
	
	/**
	 * @return the navigableCalendar
	 */
	public NavigableCalendar getNavigableCalendar() {
		return navigableCalendar;
	}

	/**
	 * @param navigableCalendar the navigableCalendar to set
	 */
	public void setNavigableCalendar(NavigableCalendar navigableCalendar) {
		this.navigableCalendar = navigableCalendar;
	}
	
}
